﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Net.Sockets;
using System.Threading;
using System.Data;
using System.Data.SqlClient;


namespace Superkatten
{
    public class Network : SQL.SQLBaseClass
    {
        public Network()
        {
            Thread Server = new Thread(new ThreadStart(server));
            Server.Start();
        }
        private void server()
        {
            TcpClient client;
            TcpListener listener;
            while (true)
            {
                IPAddress localAddress = IPAddress.Parse("127.0.0.1");
                listener = new TcpListener(localAddress, 673);
                listener.Stop();
                listener.Start();
 
                // Start listening for connections.
                Console.WriteLine("Server is waiting on socket {0}", listener.LocalEndpoint);
 
                // The program is suspended while waiting for an incoming connection.
                // This is a synchronous TCP application
                client = listener.AcceptTcpClient();
 
                // Obtain a stream object for reading and writing
                NetworkStream io = client.GetStream();
 
                 // An incoming connection needs to be processed.
                 Console.WriteLine("Received Connection from {0}", client.Client.RemoteEndPoint);
 
                 //receiving the message
                byte[] data = new byte[1024];
                string[] Received = new string[5];
                int receivedDataLength;
                //0 Name, 1 Mail, 2 Tele, 3 adress, 4 score.
                for (int i = 0; i < 5; i++)
                {
                    receivedDataLength = io.Read(data, 0, data.Length);
                    Received[i] = System.Text.Encoding.ASCII.GetString(data, 0, receivedDataLength);
                }
                Thread.Sleep(50);
                data = System.Text.Encoding.ASCII.GetBytes("Message Received");
                io.Write(data, 0, data.Length);
                for (int i = 0; i < 5; i++)
                {
                    Debug.Print(Received[i]);
                }
                Debug.Print("Ending the connection");
                // End of the incomming connection
                client.Close();
                listener.Stop();
                StoreScore(Received[0], Received[1], Received[2], Received[3], Convert.ToInt64(Received[4]));
            }
        }
        private void StoreScore(string Name, string Mail, string Tele, string Adress, long Score)
        {
            //SQL command
            SqlCommand SQLCom = new SqlCommand(@"INSERT INTO Score (Nickname, Mail, TeleNR, Adress, Score)  " +
                    "VALUES('" + @Name + "', '" + @Mail + "', '" + @Tele + "', '" + @Adress + "', " + @Score + ")");
                //get the Database connection.
                SQLCom.Connection = DatabaseConnection;
                SQLCom.Connection.Open();

                //To avoid sql injection.
                SQLCom.Parameters.Add("@Name", SqlDbType.NVarChar);
                SQLCom.Parameters.Add("@Mail", SqlDbType.NVarChar);
                SQLCom.Parameters.Add("@Tele", SqlDbType.NVarChar);
                SQLCom.Parameters.Add("@Adress", SqlDbType.NVarChar);
                SQLCom.Parameters.Add("@Score", SqlDbType.BigInt);

                SQLCom.Parameters["@Name"].Value = Name;
                SQLCom.Parameters["@Mail"].Value = Mail;
                SQLCom.Parameters["@Tele"].Value = Tele;
                SQLCom.Parameters["@Adress"].Value = Adress;
                SQLCom.Parameters["@Score"].Value = Score;

                //Execute the SQL command.
                SQLCom.ExecuteNonQuery();
                SQLCom.Connection.Close();
        }
    }
}

    